package com.blackcat.recode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackcat.recode.common.result.AjaxResult;
import com.blackcat.recode.dao.*;
import com.blackcat.recode.entity.*;
import com.blackcat.recode.service.ProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blackcat.recode.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p> 项目信息表 服务实现类
 * @author blackcat 2024-09-10
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ServiceMapper serviceMapper;
    @Autowired
    private BranchMapper branchMapper;
    @Autowired
    private UpdateRecodeMapper updateRecodeMapper;
    @Autowired
    private DeployRecodeMapper deployRecodeMapper;

    @Override
    public AjaxResult remove(String id) {
        projectMapper.deleteById(id);
        QueryWrapper<ServiceInfo> serviceInfoQueryWrapper = new QueryWrapper<>();
        serviceInfoQueryWrapper.eq("project_id", id);
        serviceMapper.delete(serviceInfoQueryWrapper);
        QueryWrapper<Branch> branchQueryWrapper = new QueryWrapper<>();
        branchQueryWrapper.eq("project_id", id);
        branchMapper.delete(branchQueryWrapper);
        QueryWrapper<UpdateRecode> updateRecodeQueryWrapper = new QueryWrapper<>();
        updateRecodeQueryWrapper.eq("project_id", id);
        updateRecodeMapper.delete(updateRecodeQueryWrapper);
        QueryWrapper<DeployRecode> deployRecodeQueryWrapper = new QueryWrapper<>();
        deployRecodeQueryWrapper.inSql("update_id", "select id from update_recode where project_id='"+id+"'");
        deployRecodeMapper.delete(deployRecodeQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getAllServiceList() {
        List<Project> result = new ArrayList<>();
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("project_name");
        List<Project> projectList = projectMapper.selectList(queryWrapper);
        if (projectList != null) {
            for (Project project : projectList) {
                QueryWrapper<ServiceInfo> serviceInfoQueryWrapper = new QueryWrapper<>();
                serviceInfoQueryWrapper.eq("project_id", project.getId());
                serviceInfoQueryWrapper.orderByAsc("service_name");
                List<ServiceInfo> serviceInfoList = serviceMapper.selectList(serviceInfoQueryWrapper);
                for (ServiceInfo serviceInfo : serviceInfoList) {
                    List<Branch> branchList = branchMapper.selectAllList(serviceInfo.getId());
                    serviceInfo.setBranchList(branchList);
                }
                project.setServiceInfoList(serviceInfoList);
                result.add(project);
            }
        }
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult getProjectNameList() {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "project_name");
        queryWrapper.orderByAsc("project_name");
        return AjaxResult.success(projectMapper.selectList(queryWrapper));
    }

    @Override
    public AjaxResult getList(Integer pageNow, Integer pageSize, String projectName, String projectType) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(projectName)) {
            queryWrapper.like("project_name", projectName);
        }
        if (StringUtils.isNotBlank(projectType)) {
            queryWrapper.eq("project_type", projectType);
        }
        queryWrapper.orderByAsc("project_name");
        Page<Project> page = new Page<>(pageNow, pageSize);
        Page<Project> result = projectMapper.selectPage(page, queryWrapper);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult projectEdit(Project project) {
        if (project.getId() == null) {
            projectMapper.insert(project);
        }else {
            projectMapper.updateById(project);
        }
        return AjaxResult.success();
    }
}
